package cn.lingyangwl.agile.model.module.mq;

import cn.lingyangwl.framework.tool.core.StringUtils;
import cn.lingyangwl.framework.tool.core.exception.BizException;
import com.alibaba.fastjson2.JSON;
import lombok.Getter;

/**
 * 定义MQ消息格式
 *
 * @author shenguangyang
 */
@Getter
public abstract class AbstractMQ {
    private final BaseMqPayload msgPayload;

    public AbstractMQ(BaseMqPayload msgPayload) {
        this.msgPayload = msgPayload;
    }



    /**
     * 构造MQ消息体 String类型
     */
    public String toMessage() {
        this.msgPayload.initWorkspace();
        return JSON.toJSONString(this.msgPayload);
    }


    public static <T extends BaseMqPayload> T parse(String msg, Class<T> tClass){
        if (StringUtils.isEmpty(msg)) {
            throw new BizException("msg is empty");
        }
        return BaseMqPayload.parse(msg, tClass);
    }
}
